package com.cheyun.netsalev3.widget

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.BR
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.MyDialogLayoutBinding
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.MyDialogViewModel
import java.io.Serializable
import kotlin.reflect.KFunction1
import com.cheyun.netsalev3.bean.DialogParam

class MyDialog : DialogFragment() {
    lateinit var binding: MyDialogLayoutBinding
    lateinit var adapter: MyAdapter
    /**系统调用这个来获得对话片段的布局，不管是否显示为对话框或嵌入片段。*/
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //将布局膨胀为对话框或嵌入片段
        binding = DataBindingUtil.inflate(inflater, R.layout.my_dialog_layout, container, false)
        var factory = InjectorUtils.getMyDialogViewModelFactory(title, list,isMultiple)
        var viewModel = ViewModelProviders.of(this, factory).get(MyDialogViewModel::class.java)
        binding.viewModel = viewModel
        initView()
        binding.setLifecycleOwner(this)
        return binding.root
    }


    private fun initView() {
        binding.cancelButton.setOnClickListener {
            dismiss()
        }
        binding.okButton.setOnClickListener {
            dismiss()
            if(!isMultiple) {
                huidia?.apply {
                    this(binding.viewModel?.getOkData())
                }
                huidiaPosition?.apply {
                    this(binding.viewModel!!.getOkPosition())
                }
            }else{
                huidiaMultiple?.apply {
                    this(binding.viewModel?.getOkDataList())
                }
            }
        }
        adapter = MyAdapter(R.layout.base_dialog_list_item2, binding.viewModel)
        binding.recyclerView.adapter = adapter
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
    }


    /**系统仅在对话框中创建布局时调用此操作。*/
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val window = dialog.window

        window.setBackgroundDrawableResource(android.R.color.transparent)
        return dialog
    }

    var title: String = ""
    var list = ArrayList<DialogParam>()
    var isMultiple:Boolean = false
    var huidia: KFunction1<@ParameterName(name = "item") DialogParam?, Unit>? = null
    var huidiaPosition: KFunction1<Int, Unit>? = null

    var huidiaMultiple: KFunction1<@ParameterName(name = "items") List<DialogParam>?, Unit>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            title = it.getString("title")
            isMultiple = it.getBoolean("isMultiple")
            list = it.getSerializable("list") as ArrayList<DialogParam>
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(title: String, list: ArrayList<DialogParam>,isMultiple:Boolean = false) =
                MyDialog().apply {
                    arguments = Bundle().apply {
                        putString("title", title)
                        putSerializable("list", list as Serializable)
                        putBoolean("isMultiple", isMultiple)
                    }
                }
    }

//    data class DialogParam(var name: String, var id: String) : BaseObservable(), Serializable {
//        override fun toString(): String {
//            return "{name:$name,id:$id,IsSelect:$IsSelect,ikey:$ikey}"
//        }
//
//        var ikey = ""
//
//        constructor(name: String, id: String, IsSelect: Boolean) : this(name, id) {
//            this.IsSelect = IsSelect
//        }
//
//        @get:Bindable
//        var IsSelect: Boolean = false
//            set(isSelect) {
//                field = isSelect
//                notifyPropertyChanged(BR.isSelect)
//            }
//
//    }


}